/**
 * 
 */
package com.mobile2you.nci.android.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.mobile2you.nci.android.domain.Annuaires;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.domain.News;
import com.mobile2you.nci.android.domain.Services;

/**
 * @author JBromo
 * 
 */
public class DataParseur {

	public static void checkUpate(Context context, String newsUpdate,
			String datakeys) {
		try {

			if (newsUpdate != null) {
				String lastUpdate = SqlHelper.getInstance(context)
						.getLastUpdate(datakeys);
				int value = 1;
				if (newsUpdate.compareToIgnoreCase(lastUpdate) != 0) {
					value = 1;
				} else {
					value = 0;
				}
				SqlHelper.getInstance(context).addUpdate(datakeys, newsUpdate,
						value);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public static List<Annuaires> parseAnnuaire(JSONArray array) {
		List<Annuaires> result = new ArrayList<Annuaires>();

		int lenght = array.length();
		for (int i = 0; i < lenght; i++) {
			try {
				JSONObject object = array.getJSONObject(i);
				Annuaires annuaires = new Annuaires(object);
				result.add(annuaires);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return result;
	}

	public static List<Centres> parseCentres(JSONArray jsonArray) {
		List<Centres> result = new ArrayList<Centres>();
		try {

			int lenght = jsonArray.length();
			for (int i = 0; i < lenght; i++) {
				try {
					JSONObject object = jsonArray.getJSONObject(i);
					Centres centres = new Centres(object);
					System.out.println("CENTRES :" + centres.toString());
					result.add(centres);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			Collections.sort(result);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static List<News> parseNews(JSONArray jsonArray) {
		List<News> result = new ArrayList<News>();
		try {

			int lenght = jsonArray.length();
			for (int i = 0; i < lenght; i++) {
				try {
					JSONObject object = jsonArray.getJSONObject(i);
					News centres = new News();
					centres.set(object);
					result.add(centres);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static List<Services> parseServices(JSONArray jsonArray) {
		List<Services> result = new ArrayList<Services>();
		try {

			int lenght = jsonArray.length();
			for (int i = 0; i < lenght; i++) {
				try {
					JSONObject object = jsonArray.getJSONObject(i);
					Services centres = new Services();
					centres.set(object);
					result.add(centres);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {

		}
		return result;
	}

	public static void parseUpdate(Context context, JSONObject object)
			throws JSONException {
		try {

			if (object != null) {
				checkUpate(context, object.getString(SqlHelper.KEY_CENTRE),
						SqlHelper.KEY_CENTRE);
				// Service
				checkUpate(context, object.getString(SqlHelper.KEY_SERVICE),
						SqlHelper.KEY_SERVICE);
				// News
				checkUpate(context, object.getString(SqlHelper.KEY_NEWS),
						SqlHelper.KEY_NEWS);
				// Annuaire
				checkUpate(context, object.getString(SqlHelper.KEY_ANNUAIRES),
						SqlHelper.KEY_ANNUAIRES);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

}
